package config;

import hibernate.IDao;

import java.util.List;

import javax.servlet.ServletConfig;

import models.Zone;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.MessageSource;
import org.springframework.web.context.ServletConfigAware;

import service.IDictService;
import service.SettingService;
import utils.StringHelper;

public class SysConfiguration implements ApplicationContextAware,ServletConfigAware {
	
	private int pageSize;
	private String rootZone;
	private String loadZone = Constant.FALSE;
	private String loadDict = Constant.FALSE;
	private String loadSetting = Constant.FALSE;

	private String showAllModules = Constant.FALSE;
	private String ignoreBadUrl= Constant.TRUE;;
	
	private String tempPath  = null;
	
	@Autowired
	@Qualifier("commonDAO")
	protected IDao dao;
	
	@Autowired
	private IDictService dictService;
	
	
	@Autowired 
	private SettingService settingService;
	@SuppressWarnings("unchecked")
	public void init() throws Exception{
		
		//系统临时目录
		Constant.TEMP_PATH = tempPath;
		
		Constant.PAGE_SIZE = pageSize;
		Constant.ROOT_ZONE_CODE = rootZone;
		
		//是否显示所有的模块菜单
		Constant.SHOW_ALL_CATEGORG = StringHelper.parseBoolean(showAllModules);
		//是否忽略不好的url菜单
		Constant.IGNORE_BLANK_URL_CATEGORG = StringHelper.parseBoolean(ignoreBadUrl);
		if(StringHelper.parseBoolean(loadZone)){
			List<Zone> list = dao.findAll("from Zone");
			for(Zone zone : list){
				if(org.apache.commons.lang.StringUtils.isNotEmpty(zone.getCode()))
					Constant.zoneMap.put(zone.getCode(), zone.getName());
			}
			list = null;
		}
		
		if(StringHelper.parseBoolean(loadDict)){
			dictService.refeshDictCache();
		}
		if(StringHelper.parseBoolean(loadSetting)){
			settingService.reLoad();
		}
		
		System.setProperty("java.awt.headless", "false");
	}

	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		Constant.AppContext = arg0;
		Constant.MESSAGE = (MessageSource)arg0.getBean("messageSource");
	}
	


	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getRootZone() {
		return rootZone;
	}

	public void setRootZone(String rootZone) {
		this.rootZone = rootZone;
	}

	public String getLoadZone() {
		return loadZone;
	}

	public void setLoadZone(String loadZone) {
		this.loadZone = loadZone;
	}

	public String getLoadDict() {
		return loadDict;
	}

	public void setLoadDict(String loadDict) {
		this.loadDict = loadDict;
	}

	public void setServletConfig(ServletConfig arg0) {
		Constant.CONFIG_PATH = arg0.getServletContext().getRealPath("/");
		
	}

	
	public String getShowAllModules() {
		return showAllModules;
	}

	public void setShowAllModules(String showAllModules) {
		this.showAllModules = showAllModules;
	}

	public String getLoadSetting() {
		return loadSetting;
	}

	public void setLoadSetting(String loadSetting) {
		this.loadSetting = loadSetting;
	}

	public String getIgnoreBadUrl() {
		return ignoreBadUrl;
	}

	public void setIgnoreBadUrl(String ignoreBadUrl) {
		this.ignoreBadUrl = ignoreBadUrl;
	}

	public String getTempPath() {
		return tempPath;
	}

	public void setTempPath(String tempPath) {
		this.tempPath = tempPath;
	}
}
